import java.util.*;
public class Testlist
{
	public static void main(String args[])
	{
		Random rdm = new Random();
		Mylist<Member> list = new Mylist<Member>();
		Mystack<Member> stack = new Mystack<Member>();
		Myqueue<Member> queue = new Myqueue<Member>();
		String msg ="Enter a command (I(nsert), R(emove), L(ist), S(tack),Q(ueue), ? or H(elp), E(xit)): ";
		char ans;
		int pos;
		Member m;
		while(true)
		{
			ans = ScreenIO.promptForChar(msg);
			switch (ans)
			{
			case 'I' : case 'i':
				    m = new Member();
			        pos = rdm.nextInt(list.size()+1);
					list.insert(m,pos);
					stack.push(m);
					queue.in(m);
					System.out.printf("%s has insert to the position %d\n",m,pos);
					break;
			case 'R' : case 'r':
				pos = ScreenIO.promptForInt("Enter a position:");
			    m = list.remove(pos);
				if(m==null)
			        System.out.printf("No object at position %d \n",pos);
				else
					System.out.printf("position %d object has removed\n",pos);
				break;
			case 'H' : case 'h':
				displayMenu();
			    break;
			case 'E': case 'e':
				System.exit(0);
			case 'L': 
				listIterm(list,true);
				break;
			case 'l':
				listIterm(list,false);
				break;
			case 'S': case 's':
				listIterm(stack,true);
				break;
			case'Q':
			    listIterm(queue,true);
			    break;
			case 'q': 
				sortedQueue(queue);
				break;
			}
		}
	}
	
	static void listIterm(Mylist<Member> lst,boolean dir)
	{
		Iterator<Member> itr = lst.iterator(dir);
		while (itr.hasNext()) System.out.println(itr.next());
	}
	static void sortedQueue(Mylist<Member> qu) 
	{
		Iterator<Member>  iterator = qu.iterator(true) ;
		List<Member> arr = new ArrayList<Member>();
		while(iterator.hasNext()){
			arr.add(iterator.next());	
		}
		Collections.sort(arr);
		Iterator<Member> iter = arr.iterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}

	}
	static void displayMenu()
	{
	System.out.println( "\t\t+==========================================");
	System.out.println( "\t\t|      Doubly Linked List Structures      |");
	System.out.println( "\t\t+------------------------------------------");
	System.out.println( "\t\t+ I/i : Insert a random member at rand pos|");
	System.out.println( "\t\t+------------------------------------------");
	System.out.println( "\t\t+ R/r : Remove an item at specified pos.  |");
	System.out.println( "\t\t+------------------------------------------");
	System.out.println( "\t\t+ L/l : List items in the list            |");
	System.out.println( "\t\t+------------------------------------------");
	System.out.println("\t\t+  S/s : List items in the stack           |");
	System.out.println("\t\t+-------------------------------------------");
	System.out.println("\t\t+  Q : List items in the Queue             |");
	System.out.println("\t\t+-------------------------------------------");
	System.out.println("\t\t+  q : List items in the SortedQueue       |");
	System.out.println("\t\t+-------------------------------------------");
	System.out.println("\t\t+  H: Help menu                            |");
	System.out.println("\t\t+-------------------------------------------");
	System.out.println("\t\t+ E:  Exit this program                    |");
	System.out.println("\t\t+===========================================");
	}
}